#include "polygon.h"

#define PI	3.14159265359f

CPolygon::CPolygon()
{
	setType(1);
	setSize(100.0);
}

void CPolygon::drawCross()
{

	// Note: Size represents the radius of the circle

	moveTo(ctr.getX(), ctr.getY() + size);								// Start at the center, then move up (90.0 deg)
	lineTo(ctr.getX(), ctr.getY() - size);								// Now move down (270.0 deg)

	moveTo(ctr.getX() + size, ctr.getY());								// Start at the center, move to the right (0.0 deg)
	lineTo(-size, ctr.getY());											// Now move left (180.0 deg)
}

void CPolygon:: setType(int pt)
{
	polyType = pt;
}

void CPolygon::setSize(float s)
{
	size = s;
}

void CPolygon::onDraw()
{
	switch(polyType)
	{
		case CIRCLE:											// A Circle is not a polygon, but is an enclosed figure (a 1-gon)
			drawCircle(ctr, size);
			break;
		case TRIANGLE:											// A Triangle is a 3-gon
			drawTriangle(ctr, size);
			break;
		case RECTANGLE:											// A Rectangle is a 4-gon
			drawRectangle(ctr, size);
			break;
	}

}
//<<moveTo>>
//changes current point
void CPolygon:: moveTo(float x, float y)
{
	drawPt.set(x, y);
}

//<<lineTo>>
//draws a line from current point to new point
void CPolygon:: lineTo(float x, float y)
{
	glBegin(GL_LINES);
		glColor3f(5.0, 0.0, 0.0);
		glVertex2f((GLfloat) drawPt.getX(), (GLfloat) drawPt.getY());
		glVertex2f((GLfloat) x, (GLfloat) y); //draw the line
	glEnd();
	glFlush();
	drawPt.set (x, y); //update current point to new point
}

void CPolygon:: draw(int n, float cx, float cy, float side, float revAngle)
{
	if(n < 3)
		return; 					// bad number of sides
	float angle = revAngle * PI / 180;  // initial angle
	float angleInc = 2 * PI / n;         //angle increment
	moveTo(side * cos(angle) + cx, side * sin(angle) + cy);
	for(int k = 0; k < n; k++)  // repeat n times
	{
		angle += angleInc; 
		lineTo(side * cos(angle) + cx, side * sin(angle) + cy);
	}
}

// Draw a square

void CPolygon::drawRectangle(Point2 pt, float side)
{
	draw(4, pt.getX(), pt.getY(), side, 90.0);
}

// Draw a triangle

void CPolygon::drawTriangle(Point2 pt, float side)
{
	draw(3, pt.getX(), pt.getY(), side, 60.0);
}

// Draw a circle

void CPolygon::drawCircle(Point2 pt, float radius)
{
	int segs = 50;
	draw(segs, pt.getX(), pt.getY(), radius, 360.0);
}